clear all
set more off
global mun "H:/Stata/Data/municipality"
global admin "H:/Stata/Data/"
global temp "H:/Stata/Data/temp"
global output "H:/Stata/Output/toexport_figures"

set scheme plotplainblind

use "$temp/admin_mun2_FINAL_urban40", clear

drop munid

encode formateur, gen(munid)
xtset munid year

replace left_pct = left_pct / 100  // so outcome between 0 and 1
replace pvda_pct = pvda_pct /100
replace christian_pct = christian_pct / 100
replace right_pct = right_pct / 100	
replace populist_pct = populist_pct/ 100

gen time = . 
replace time = 1 if year == 1998
replace time = 2 if year == 2002
replace time = 3 if year == 2003
replace time = 4 if year == 2006
replace time = 5 if year == 2010
replace time = 6 if year == 2012
replace time = 7 if year == 2017


xtset munid time 

gen  lag_treat_pop = l.treat_total_2C_pop 
gen  treat_change_pop = treat_total_2C_pop - lag_treat_pop

egen treat_sd = std(treat_total_2C_pop)
egen treat_change_sd = std(treat_change_pop)


gen  lag_control_pop = l.control_total_2C_pop
gen  control_change_pop = control_total_2C_pop - lag_control_pop

egen control_sd = std(control_total_2C_pop)
egen control_change_sd = std(control_change_pop)


gen  lag_treat_sociobefore = l.treat_socio_before_pop
gen  treat_change_sociobefore = treat_socio_before_pop - lag_treat_sociobefore
egen treat_sociobefore_change_sd = std(treat_change_sociobefore)

gen  lag_treat_sociobefore5yo = l.treat_socio_before5yo_pop
gen  treat_change_sociobefore5yo = treat_socio_before5yo_pop - lag_treat_sociobefore5yo
egen treat_sociobefore5yo_change_sd = std(treat_change_sociobefore5yo)


gen  lag_treat_sociobefore10yo = l.treat_socio_before10yo_pop
gen  treat_change_sociobefore10yo = treat_socio_before10yo_pop - lag_treat_sociobefore10yo
egen treat_sociobefore10yo_change_sd = std(treat_change_sociobefore10yo)


gen  lag_treat_socioafter = l.treat_socio_after_pop
gen  treat_change_socioafter = treat_socio_after_pop - lag_treat_socioafter
egen treat_socioafter_change_sd = std(treat_change_socioafter)

gen  lag_treat_socioafter2yo = l.treat_socio_after2yo_pop
gen  treat_change_socioafter2yo = treat_socio_after2yo_pop - lag_treat_socioafter2yo
egen treat_socioafter2yo_change_sd = std(treat_change_socioafter2yo)

gen  lag_treat_socioafter10yo = l.treat_socio_after10yo_pop
gen  treat_change_socioafter10yo = treat_socio_after10yo_pop - lag_treat_socioafter10yo
egen treat_socioafter10yo_change_sd = std(treat_change_socioafter10yo)


* socialization?

** west pre vs treat (geography)

reg left_pct  treat_sociobefore10yo_change_sd treat_sociobefore5yo_change_sd  treat_sociobefore_change_sd  treat_change_sd   i.year   ,  cl(munid) 
regsave treat_sociobefore10yo_change_sd treat_sociobefore5yo_change_sd treat_sociobefore_change_sd  treat_change_sd using "$temp/regsave1",  replace ci

** west treat vs post (socialization)

reg left_pct treat_change_sd treat_socioafter2yo_change_sd treat_socioafter_change_sd  treat_socioafter10yo_change_sd    i.year  ,  cl(munid) 
regsave treat_change_sd treat_socioafter2yo_change_sd treat_socioafter_change_sd  treat_socioafter10yo_change_sd   using "$temp/regsave2",  replace ci


************************************************************
* Table 1: Famine Treatment Effect on Left Vote Share
************************************************************

reg left_pct treat_change_sd control_change_sd i.year  ,  cl(munid)
outreg2 using "$output/tab1.doc", keep(treat_change_sd control_change_sd) replace dec(3)

reg left_pct  treat_sociobefore10yo_change_sd   treat_change_sd   i.year   ,  cl(munid) 
outreg2 using "$output/tab1.doc", keep(treat_sociobefore10yo_change_sd   treat_change_sd) append dec(3)

reg left_pct treat_change_sd   treat_socioafter10yo_change_sd    i.year ,  cl(munid) 
outreg2 using "$output/tab1.doc", keep(treat_change_sd   treat_socioafter10yo_change_sd )  dec(3) append tex


************************************************************
* Table 2: Left Support Among Treated Conceived Before the Onset of the Famine and Gender
************************************************************

* conception? 

gen  lag_treat_conception = l.treat_conception_pop
gen  treat_change_conception = treat_conception_pop - lag_treat_conception
egen treat_conception_change_sd = std(treat_change_conception)


gen  lag_control_conception = l.control_conception_pop
gen  control_change_conception = control_conception_pop - lag_control_conception
egen control_conception_change_sd = std(control_change_conception)


reg left_pct treat_conception_change_sd control_conception_change_sd i.year,  cl(munid)
outreg2 using "$output/mun_urban40_pop_several.doc", replace keep(treat_conception_change_sd control_conception_change_sd) dec(3)

* male female? 


gen lag_treat_male_pop = l.treat_male_pop
gen treat_change_male_pop = treat_male_pop - lag_treat_male_pop
egen treat_change_male_pop_sd = std(treat_change_male_pop)


gen lag_control_male_pop = l.control_male_pop
gen control_change_male_pop = control_male_pop - lag_control_male_pop
egen control_change_male_pop_sd = std(control_change_male_pop)

reg left_pct treat_change_male_pop_sd control_change_male_pop_sd i.year,  cl(munid)
outreg2 using "$output/mun_urban40_pop_several.doc", append keep(treat_change_male_pop_sd control_change_male_pop_sd ) dec(3)



gen lag_treat_female_pop = l.treat_female_pop
gen treat_change_female_pop = treat_female_pop - lag_treat_female_pop
egen treat_change_female_pop_sd = std(treat_change_female_pop)

gen lag_control_female_pop = l.control_female_pop
gen control_change_female_pop = control_female_pop - lag_control_female_pop
egen control_change_female_pop_sd = std(control_change_female_pop)


reg left_pct treat_change_female_pop_sd control_change_female_pop_sd i.year,  cl(munid)
outreg2 using "$output/mun_urban40_pop_several.doc",  keep(treat_change_female_pop_sd control_change_female_pop_sd  ) dec(3) append tex 

************************************************************
* Table 3: Residence Change Effect on Treated Density and Left Support
************************************************************

xtset munid year

* Movers and Treated and Left
egen movers_sd = std(movers_pop)
xtreg treat_sd movers_sd i.year , fe  cl(munid)
outreg2 using "$output/tab3.doc", replace dec(3)
xtreg left_pct movers_sd  i.year ,fe   cl(munid)
outreg2 using "$output/tab3.doc", dec(3) append tex

************************************************************
* Table B3: Main Results with Time-Varying Controls and Alternative Specification
************************************************************

global controls loghome  logspend female_pop foreign_pop

reg left_pct treat_change_sd control_change_sd i.year  ,  cl(munid)
outreg2 using "$output/tabb3.doc", replace dec(3)
reg left_pct treat_change_sd control_change_sd i.year   $controls , cl(munid)
estimate store muncoef_main
outreg2 using "$output/tabb3.doc", append dec(3)

xtreg left_pct treat_sd control_sd i.year , fe cl(munid)
outreg2 using "$output/tabb3.doc", append dec(3)
xtreg left_pct treat_sd control_sd i.year   $controls, fe cl(munid)
outreg2 using "$output/tabb3.doc",  dec(3) append tex


************************************************************
* Table B2: Main Specification Illustration with Time-Varying Controls
************************************************************
program define tabb2
preserve
use "$temp/admin_mun2_FINAL_urban40", clear
drop munid

encode formateur, gen(munid)
xtset munid year

preserve
gen logpop = log(pop)

replace avhomeprice_adjusted = "" if avhomeprice_adjusted == "NA"
destring avhomeprice_adjusted, replace
drop if avhomeprice_adjusted == . // 29 obs this is the sample for analysis

gen loghome = log(avhomeprice_adjusted)


gen spending_national = 39346000000 if year == 1998
replace spending_national = 47888000000 if year == 2002
replace spending_national = 48663000000 if year == 2003
replace spending_national = 49740000000 if year == 2006
replace spending_national = 57003000000 if year == 2010
replace spending_national = 53631000000 if year == 2012
replace spending_national = 58759000000 if year == 2017

bysort munid year: gen spend_mun = spending_national/pop

gen logspend = log(spend_mun)


global controls loghome  logspend female_pop foreign_pop 

replace left_pct = left_pct / 100  // so outcome between 0 and 1
replace pvda_pct = pvda_pct /100
replace christian_pct = christian_pct / 100
replace right_pct = right_pct / 100	
replace populist_pct = populist_pct/ 100

gen time = . 
replace time = 1 if year == 1998
replace time = 2 if year == 2002
replace time = 3 if year == 2003
replace time = 4 if year == 2006
replace time = 5 if year == 2010
replace time = 6 if year == 2012
replace time = 7 if year == 2017


xtset munid time 

gen  lag_treat_pop = l.treat_total_2C_pop // no more gen double (it didnt get right the decimals)
gen  treat_change_pop = treat_total_2C_pop - lag_treat_pop

egen treat_sd = std(treat_total_2C_pop)
egen treat_change_sd = std(treat_change_pop)

gen  lag_control_pop = l.control_total_2C_pop
gen  control_change_pop = control_total_2C_pop - lag_control_pop

egen control_sd = std(control_total_2C_pop)
egen control_change_sd = std(control_change_pop)


reg left_pct treat_change_sd control_change_sd i.year  ,  cl(munid)
outreg2 using "$output/tabb2.doc", replace dec(3)
reg left_pct treat_change_sd control_change_sd i.year $controls  ,  cl(munid)
outreg2 using "$output/tabb2.doc",  dec(3) append tex
restore		
end
tabb2 



************************************************************
* Table C2: Famine Effect on Party Support (Budget Spending)
************************************************************

program define tabc2
preserve

use "$temp/admin_mun2_FINAL_urban40", clear
drop munid

encode formateur, gen(munid)
xtset munid year


replace left_pct = left_pct / 100  // so outcome between 0 and 1
replace pvda_pct = pvda_pct /100

replace pvdd_pct = "." if pvdd_pct == "NA"
destring pvdd_pct, replace
replace pvdd_pct = pvdd_pct /100

replace plus50_pct = plus50_pct / 100

gen ln_pct = ln / validvotes * 100
replace ln_pct = ln_pct / 100

replace green_pct = green_pct / 100
replace sp_pct = sp_pct / 100
replace cda_pct = cda_pct / 100
replace d66_pct = d66_pct / 100
replace cu_pct = cu_pct / 100
replace sgp_pct = sgp_pct / 100
replace pvv_pct = pvv_pct / 100
replace vvd_pct = vvd_pct / 100



gen time = . 
replace time = 1 if year == 1998
replace time = 2 if year == 2002
replace time = 3 if year == 2006
replace time = 4 if year == 2010
replace time = 5 if year == 2012
replace time = 6 if year == 2017


xtset munid time 



gen lag_treat_pop = l.treat_total_2C_pop
gen treat_change_pop = treat_total_2C_pop - lag_treat_pop

egen treat_change_sd = std(treat_change_pop)


gen  lag_control_pop = l.control_total_2C_pop
gen  control_change_pop = control_total_2C_pop - lag_control_pop

egen control_change_sd = std(control_change_pop)

	
***********************
** Health Budget Data
***********************

gen health = . 
	replace health = green_pct +  pvda_pct + d66_pct if year	== 1998
	replace health = green_pct + sp_pct  if year	== 2002 // 2003 snap election, no data
	replace health = green_pct + sp_pct + pvda_pct if year	== 2006
	replace health = green_pct + sp_pct + pvv_pct if year	== 2010
	replace health = green_pct + sp_pct + sgp_pct + pvv_pct if year	== 2012
	replace health = green_pct + sp_pct + pvda_pct + sgp_pct if year	== 2017

reg health treat_change_sd control_change_sd i.year,  cl(munid)
outreg2 using "$output/tabc2.doc", keep(treat_change_sd control_change_sd) replace dec(3)


gen health_median = .
	replace health_median = green_pct + pvda_pct + d66_pct if year	== 1998
	replace health_median = green_pct + sp_pct + pvda_pct + cda_pct if year	== 2002
	replace health_median = green_pct + sp_pct + pvda_pct + sgp_pct if year	== 2006
	replace health_median = green_pct + sp_pct + pvda_pct+ pvv_pct + cu_pct if year	== 2010
	replace health_median = green_pct + sp_pct + pvv_pct + sgp_pct if year	== 2012
	replace health_median = green_pct + sp_pct + pvda_pct + cda_pct if year	== 2017

reg health_median treat_change_sd control_change_sd i.year,  cl(munid)
outreg2 using "$output/tabc2.doc", keep(treat_change_sd control_change_sd) append dec(3)

	
	
gen welfare = .
		replace welfare = green_pct if year == 1998
		replace welfare = green_pct + sp_pct + sgp_pct if year == 2002
		replace welfare = green_pct + sp_pct + pvda_pct + cda_pct if year == 2006
		replace welfare = green_pct + sp_pct + pvv_pct + cu_pct + pvda_pct if year == 2010
		replace welfare = cu_pct + sgp_pct + pvv_pct if year == 2012
		replace welfare = sp_pct + pvda_pct + cda_pct + d66_pct if year == 2017
	
		
reg welfare treat_change_sd control_change_sd  i.year,  cl(munid)
outreg2 using "$output/tabc2.doc", keep(treat_change_sd control_change_sd) append dec(3)



reg welfare treat_change_sd control_change_sd  i.year $controls c.year##i.munid, cl(munid) // works

		
gen welfare_median = .
	replace welfare_median = green_pct + cda_pct if year == 1998
	replace welfare_median = green_pct + sp_pct +  pvda_pct+ sgp_pct if year == 2002
	replace welfare_median = green_pct + sp_pct + pvda_pct + cda_pct if year == 2006
	replace welfare_median = green_pct + sp_pct + pvda_pct + pvv_pct + cu_pct  if year == 2010
	replace welfare_median = cda_pct + pvv_pct + cu_pct + sgp_pct if year == 2012
	replace welfare_median = sp_pct + pvda_pct + cda_pct + d66_pct if year == 2017

	
reg welfare_median treat_change_sd control_change_sd i.year,  cl(munid)
outreg2 using "$output/tabc2.doc", keep(treat_change_sd control_change_sd) append dec(3)




			
*placebo


gen order = . 
		replace order = vvd_pct + d66_pct + pvda_pct + cda_pct if year == 1998
		replace order = sgp_pct + pvda_pct + vvd_pct + cda_pct if year == 2002
		replace order = sgp_pct + vvd_pct + green_pct + cu_pct + sp_pct if year == 2006
		replace order = pvda_pct + sp_pct + pvv_pct + sgp_pct + vvd_pct if year == 2010
		replace order =  cu_pct + sgp_pct + green_pct + sp_pct + vvd_pct if year == 2012
		replace order = sp_pct + vvd_pct + sgp_pct if year == 2017
		
		
reg order treat_change_sd control_change_sd i.year,  cl(munid)
outreg2 using "$output/tabc2.doc", keep(treat_change_sd control_change_sd) append dec(3)


	
gen order_median = . 
		replace order_median = vvd_pct + d66_pct + pvda_pct + cda_pct if year == 1998
		replace order_median = sgp_pct + pvda_pct + vvd_pct + cda_pct if year == 2002
		replace order_median = green_pct + sp_pct + vvd_pct +  cu_pct + sgp_pct if year == 2006
		replace order_median = sgp_pct + sp_pct + pvda_pct + pvv_pct + vvd_pct if year == 2010
		replace order_median = vvd_pct + sp_pct + cu_pct + sgp_pct + green_pct if year == 2012
		replace order_median = sp_pct + vvd_pct + sgp_pct if year == 2017

reg order_median treat_change_sd control_change_sd i.year,  cl(munid)
outreg2 using "$output/tabc2.doc", keep(treat_change_sd control_change_sd)  dec(3) append tex
restore
end
tabc2

	
************************************************************
* Table C1: Famine Effect on Left Support (CMP)
************************************************************

program define tabc1
preserve 
use "$temp/admin_mun2_FINAL_urban40", clear
drop munid

encode formateur, gen(munid)
xtset munid year


replace left_pct = left_pct / 100  // so outcome between 0 and 1
replace pvda_pct = pvda_pct /100

replace pvdd_pct = "." if pvdd_pct == "NA"
destring pvdd_pct, replace
replace pvdd_pct = pvdd_pct /100

replace plus50_pct = plus50_pct / 100

gen ln_pct = ln / validvotes * 100
replace ln_pct = ln_pct / 100

replace green_pct = green_pct / 100
replace sp_pct = sp_pct / 100
replace cda_pct = cda_pct / 100
replace d66_pct = d66_pct / 100
replace cu_pct = cu_pct / 100
replace sgp_pct = sgp_pct / 100
replace pvv_pct = pvv_pct / 100
replace lpf_pct = lpf_pct / 100
replace vvd_pct = vvd_pct / 100 


gen time = . 
replace time = 1 if year == 1998
replace time = 2 if year == 2002
replace time = 3 if year == 2003
replace time = 4 if year == 2006
replace time = 5 if year == 2010
replace time = 6 if year == 2012
replace time = 7 if year == 2017

xtset munid time 

gen  lag_treat_pop = l.treat_total_2C_pop
gen  treat_change_pop = treat_total_2C_pop - lag_treat_pop

egen treat_change_sd = std(treat_change_pop)


gen  lag_control_pop = l.control_total_2C_pop
gen  control_change_pop = control_total_2C_pop - lag_control_pop

egen control_change_sd = std(control_change_pop)


********************
** Manifesto Data
********************


** redistributive parties AVERAGE (all, exlcuding small parties <3%) 

gen Rparty_pct = green_pct + sp_pct if year == 1998
replace Rparty_pct = green_pct + sp_pct + pvda_pct + d66_pct +cda_pct + cu_pct + sgp_pct if year == 2002
replace Rparty_pct = green_pct + sp_pct + pvda_pct + d66_pct + ln_pct if year == 2003
replace Rparty_pct = green_pct + sp_pct + pvda_pct + cu_pct + pvdd_pct if year == 2006
replace Rparty_pct = green_pct + sp_pct + pvda_pct +  pvdd_pct + sgp_pct if year == 2010
replace Rparty_pct = green_pct + sp_pct + pvda_pct +  pvdd_pct + plus50_pct if year == 2012
replace Rparty_pct = green_pct + sp_pct + pvda_pct  + pvdd_pct + plus50_pct if year == 2017


reg Rparty_pct treat_change_sd control_change_sd i.year,  cl(munid)
outreg2 using "$output/tabc1.doc", keep(treat_change_sd control_change_sd) replace dec(3)


** redistributive party MEDIAN (all, excluding small parties <3%) 

gen Rparty_median = green_pct + sp_pct + pvda_pct + cda_pct if year == 1998
replace Rparty_median = green_pct + sp_pct + pvda_pct + d66_pct  + cu_pct if year == 2002
replace Rparty_median = green_pct + sp_pct  + d66_pct + ln_pct if year == 2003
replace Rparty_median = green_pct + sp_pct + pvda_pct + cu_pct + pvdd_pct if year == 2006
replace Rparty_median = green_pct + sp_pct + pvda_pct +  pvdd_pct + sgp_pct if year == 2010
replace Rparty_median = green_pct + sp_pct + pvda_pct +  pvdd_pct + plus50_pct if year == 2012
replace Rparty_median = green_pct + sp_pct + pvda_pct +  cda_pct + pvdd_pct + plus50_pct if year == 2017


reg Rparty_median treat_change_sd control_change_sd i.year,  cl(munid)
outreg2 using "$output/tabc1.doc", keep(treat_change_sd control_change_sd) append dec(3)



** redistributive parties MEAN (pro-welfare)

gen wsparty = green_pct + sp_pct + sgp_pct + cda_pct if year == 1998
replace wsparty = green_pct + sp_pct + pvda_pct + sgp_pct + cu_pct if year == 2002
replace wsparty = green_pct + sp_pct + pvda_pct  if year == 2003
replace wsparty = green_pct + sp_pct + pvda_pct +  cu_pct + pvdd_pct if year == 2006
replace wsparty = green_pct + sp_pct  + sgp_pct  if year == 2010
replace wsparty = green_pct + sp_pct + pvda_pct + pvdd_pct + plus50_pct + pvv_pct if year == 2012
replace wsparty = green_pct + sp_pct + pvda_pct + cda_pct + pvdd_pct + plus50_pct + cu_pct if year == 2017


reg wsparty treat_change_sd control_change_sd i.year,  cl(munid)
outreg2 using "$output/tabc1.doc", keep(treat_change_sd control_change_sd) append dec(3)



** redistributive parties MEDIAN (pro-welfare)

gen wsparty_median = green_pct + sp_pct + sgp_pct + cda_pct if year == 1998
replace wsparty_median = green_pct + sp_pct + pvda_pct + sgp_pct + cu_pct if year == 2002
replace wsparty_median = green_pct + sp_pct + pvda_pct +ln_pct if year == 2003
replace wsparty_median = green_pct + sp_pct + pvda_pct +  cu_pct + pvdd_pct if year == 2006
replace wsparty_median = green_pct + sp_pct  + sgp_pct + pvda_pct + pvv_pct  if year == 2010
replace wsparty_median = sp_pct + pvda_pct + pvdd_pct + plus50_pct + pvv_pct if year == 2012
replace wsparty_median = green_pct + sp_pct +  cda_pct + pvdd_pct  + cu_pct if year == 2017

reg wsparty_median treat_change_sd control_change_sd i.year,  cl(munid)
outreg2 using "$output/tabc1.doc", keep(treat_change_sd control_change_sd) append dec(3)


** insurance parties MEAN

gen insparty = green_pct + sp_pct + sgp_pct if year == 1998
replace insparty = green_pct + sp_pct + pvda_pct + sgp_pct + cu_pct + d66_pct + cda_pct if year == 2002
replace insparty = green_pct + sp_pct + cda_pct + ln_pct if year == 2003
replace insparty = green_pct + sp_pct + pvda_pct + cu_pct + pvdd_pct if year == 2006
replace insparty = green_pct + sp_pct + pvdd_pct if year == 2010
replace insparty = green_pct + sp_pct + pvda_pct + pvdd_pct + plus50_pct + pvv_pct if year == 2012
replace insparty = green_pct + sp_pct + pvda_pct + cda_pct + pvdd_pct + plus50_pct if year == 2017

reg insparty treat_change_sd control_change_sd i.year,  cl(munid)
outreg2 using "$output/tabc1.doc", keep(treat_change_sd control_change_sd) append dec(3)



** insurance parties MEDIAN

gen insparty_median = green_pct + sp_pct + sgp_pct + cda_pct if year == 1998
replace insparty_median = green_pct + sp_pct + pvda_pct + sgp_pct + cu_pct  if year == 2002
replace insparty_median = green_pct + sp_pct + pvda_pct + ln_pct if year == 2003
replace insparty_median = green_pct + sp_pct + pvda_pct + cu_pct + pvdd_pct if year == 2006
replace insparty_median = green_pct + sp_pct + sgp_pct + pvda_pct + pvv_pct if year == 2010
replace insparty_median = sp_pct + pvda_pct + pvdd_pct + plus50_pct + pvv_pct if year == 2012
replace insparty_median = green_pct + sp_pct +  cda_pct + pvdd_pct + plus50_pct + pvda_pct if year == 2017

reg insparty_median treat_change_sd control_change_sd i.year,  cl(munid)
outreg2 using "$output/tabc1.doc", keep(treat_change_sd control_change_sd) dec(3) append tex
restore
end
tabc1


************************************************************
* Table B1: Famine Effect on Left Vote Share Alternative Urban and Excluding the South
************************************************************
program define tabb1
preserve

** defining treated

use "$temp/admin_mun_v2", clear

*Treatment Based on Urban 3040

gen treatdefine_total_2C_3040 = 1 if yob == 1944 & mob == 11 & all_urban_3040  == 1 & region == 1 
replace treatdefine_total_2C_3040 = 1 if yob == 1944 & mob == 12 & all_urban_3040  == 1 & region == 1 
replace treatdefine_total_2C_3040 = 1 if yob == 1945  & all_urban_3040  == 1 & region == 1 
replace treatdefine_total_2C_3040 = 1 if yob == 1946 & mob == 1 & all_urban_3040  == 1 & region == 1 

replace treatdefine_total_2C_3040 = 0 if yob == 1944 & mob == 11 & all_urban_3040  == 1 & region == 0
replace treatdefine_total_2C_3040 = 0 if yob == 1944 & mob == 12 & all_urban_3040  == 1 & region == 0
replace treatdefine_total_2C_3040 = 0 if yob == 1945 & all_urban_3040  == 1 & region == 0
replace treatdefine_total_2C_3040 = 0 if yob == 1946 & mob == 1& all_urban_3040  == 1 & region == 0

** defining control

gen control_total_2C_3040 = 1 if  treatdefine_total_2C_3040 == 0 
replace control_total_2C_3040 = 0 if treatdefine_total_2C_3040 == 1


*Treatment Based on Urban but Not South

gen treatdefine_total_2C_nosouth = 1 if yob == 1944 & mob == 11 & all_urban_40  == 1 & region3 == 1 
replace treatdefine_total_2C_nosouth = 1 if yob == 1944 & mob == 12 & all_urban_40  == 1 & region3 == 1 
replace treatdefine_total_2C_nosouth = 1 if yob == 1945  & all_urban_40  == 1 & region3 == 1 
replace treatdefine_total_2C_nosouth = 1 if yob == 1946 & mob == 1 & all_urban_40  == 1 & region3 == 1 

replace treatdefine_total_2C_nosouth = 0 if yob == 1944 & mob == 11 & all_urban_40  == 1 & region3 == 2
replace treatdefine_total_2C_nosouth = 0 if yob == 1944 & mob == 12 & all_urban_40  == 1 & region3 == 2
replace treatdefine_total_2C_nosouth = 0 if yob == 1945 & all_urban_40  == 1 & region3 == 2
replace treatdefine_total_2C_nosouth = 0 if yob == 1946 & mob == 1& all_urban_40  == 1 & region3 == 2

*defining control

gen control_total_2C_nosouth = 1 if  treatdefine_total_2C_nosouth == 0 
replace control_total_2C_nosouth = 0 if treatdefine_total_2C_nosouth == 1

egen double group = group(RINPERSOON RINPERSOONS)

** keeping only those who are above 18 (voting age)
drop age 
gen age = year - yob
 
gen adult = 1 if age >=18 & age!=. 

keep if adult == 1 
drop adult
 
bysort formateur year: egen mun_people_count = count(group) // total people (RINPERSOON RINPERSOONS) in municipality



*total urban3040
bysort formateur year: egen treat_total_2C_3040_agg_treated = sum (treatdefine_total_2C_3040) // number of treated
gen treat_total_2C_3040_pop = treat_total_2C_3040_agg_treated/mun_people_count 
drop   treatdefine_total_2C_3040 treat_total_2C_3040_agg_treated

bysort formateur year: egen control_3040_agg_treated = sum (control_total_2C_3040) // number of treated
gen control_total_2C_3040_pop = control_3040_agg_treated/mun_people_count 
drop control_3040_agg_treated control_total_2C_3040

*total nosouth
bysort formateur year: egen treat_nosouth_agg_treated = sum (treatdefine_total_2C_nosouth) // number of treated
gen treat_total_2C_nosouth_pop = treat_nosouth_agg_treated/mun_people_count 
drop   treatdefine_total_2C_nosouth treat_nosouth_agg_treated

bysort formateur year: egen control_nosouth_agg_treated = sum (control_total_2C_nosouth) // number of treated
gen control_total_2C_nosouth_pop = control_nosouth_agg_treated/mun_people_count 
drop control_nosouth_agg_treated control_total_2C_nosouth


duplicates drop formateur year, force

merge 1:1 formateur year using "$mun/municipality_v2" // bringing in mun vars
keep if _merge == 3 // 2715
drop _merge

drop RINPERSOON RINPERSOONS group

order residence formateur year


drop munid
encode formateur, gen(munid)


replace left_pct = left_pct / 100  // so outcome between 0 and 1
replace pvda_pct = pvda_pct /100
replace christian_pct = christian_pct / 100
replace right_pct = right_pct / 100	
replace populist_pct = populist_pct/ 100

gen time = . 
replace time = 1 if year == 1998
replace time = 2 if year == 2002
replace time = 3 if year == 2003
replace time = 4 if year == 2006
replace time = 5 if year == 2010
replace time = 6 if year == 2012
replace time = 7 if year == 2017


xtset munid time // not year because there's gaps in year

gen  lag_treat_3040_pop = l.treat_total_2C_3040_pop // no more gen double (it didnt get right the decimals)
gen  treat_change_3040_pop = treat_total_2C_3040_pop - lag_treat_3040_pop

egen treat_3040_sd = std(treat_total_2C_3040_pop)
egen treat_change_3040_sd = std(treat_change_3040_pop)

gen  lag_control_3040_pop = l.control_total_2C_3040_pop
gen  control_change_3040_pop = control_total_2C_3040_pop - lag_control_3040_pop

egen control_3040_sd = std(control_total_2C_3040_pop)
egen control_change_3040_sd = std(control_change_3040_pop)



reg left_pct treat_change_3040_sd control_change_3040_sd i.year  ,  cl(munid)
outreg2 using "$output/tabb1.doc", keep(treat_change_3040_sd control_change_3040_sd) replace dec(3)


*no south
gen  lag_treat_nosouth_pop = l.treat_total_2C_nosouth_pop // no more gen double (it didnt get right the decimals)
gen  treat_change_nosouth_pop = treat_total_2C_nosouth_pop - lag_treat_nosouth_pop

egen treat_nosouth_sd = std(treat_total_2C_nosouth_pop)
egen treat_change_nosouth_sd = std(treat_change_nosouth_pop)

gen  lag_control_nosouth_pop = l.control_total_2C_nosouth_pop
gen  control_change_nosouth_pop = control_total_2C_nosouth_pop - lag_control_nosouth_pop

egen control_nosouth_sd = std(control_total_2C_nosouth_pop)
egen control_change_nosouth_sd = std(control_change_nosouth_pop)


reg left_pct treat_change_nosouth_sd control_change_nosouth_sd i.year  ,  cl(munid)
outreg2 using "$output/tabb1.doc", keep(treat_change_nosouth_sd control_change_nosouth_sd)  dec(3) append tex

restore
end
tabb1


******************
** Descriptives 
******************

preserve

use "$temp/admin_mun2_FINAL_urban40", clear
drop munid

encode formateur, gen(munid)
xtset munid year

gen logpop = log(pop)
replace avhomeprice_adjusted = "" if avhomeprice_adjusted == "NA"

destring avhomeprice_adjusted, replace
gen loghome = log(avhomeprice_adjusted) // not dropping missing so i have full mun sample

replace left_pct = left_pct / 100  // so outcome between 0 and 1
replace pvda_pct = pvda_pct /100

gen time = . 
replace time = 1 if year == 1998
replace time = 2 if year == 2002
replace time = 3 if year == 2003
replace time = 4 if year == 2006
replace time = 5 if year == 2010
replace time = 6 if year == 2012
replace time = 7 if year == 2017

xtset munid time 

 
gen  lag_treat_pop = l.treat_total_2C_pop // no more gen double (it didnt get right the decimals)
gen  treat_change_pop = treat_total_2C_pop - lag_treat_pop
egen treat_change_sd = std(treat_change_pop)


* Are municipalities in which treated numbers increase?

xtset munid time

bysort  time: egen meanTREAT= mean(treat_total_2C_pop)

bysort  time: egen meanLEFT= mean(left_pct)


sort munid time


** changes in municipality from 1 point to another

xtset munid time // not year because there's gaps in year
gen  lag_left = l.left_pct
gen  left_change = left_pct - lag_left

*change at any given point in time (i.e. they change at least once)
gen treat_typechange = 0 if  treat_change_pop == 0
replace treat_typechange = 1 if treat_change_pop >0
replace treat_typechange = -1 if treat_change_pop <0

tab treat_typechange

gen left_typechange = 0 if  left_change == 0
replace left_typechange = 1 if left_change >0
replace left_typechange = -1 if left_change <0

tab left_typechange treat_typechange


** which municipalities are the most stable? 
gen  treat_change_sd_std = abs(treat_change_sd)

* stable less than 1 SD change in treatment 
gen change_less1sd = 1 if treat_change_sd_std <= 1  & treat_change_sd_std !=.
replace change_less1sd = 0 if treat_change_sd_std >1 & treat_change_sd_std !=.


* stable less than 2SD change in treatment
gen change_less2sd = 1 if treat_change_sd_std <= 2  & treat_change_sd_std !=.
replace change_less2sd = 0 if treat_change_sd_std >2 & treat_change_sd_std !=.


save "H:/Stata/Data/temp/toplot", replace


** data for the map 

*getting average levels
bysort  munid: egen meanMUN_treat= mean(treat_total_2C_pop) 
bysort  munid: egen meanMUN_left= mean(left_pct)

*getting average change

bysort munid: egen meanMUN_treatchange = mean(treat_change_pop)
bysort munid: egen meanMUN_leftchange = mean(left_change)

*getting average level (absolute)

bysort munid: egen meanMUN_treatagg = mean(treat_total_2C_agg_treated)

*getting average change (absolute)
gen  lag_treat_agg = l.treat_total_2C_agg_treated
gen  treat_agg_change = treat_total_2C_agg_treated - lag_treat_agg

bysort munid: egen meanMUN_treataggch = mean(treat_agg_change)

duplicates drop meanMUN_treat meanMUN_left meanMUN_treatchange meanMUN_leftchange meanMUN_treataggch meanMUN_treatagg , force
rename formateur munname
keep munid meanMUN_treat meanMUN_left munname meanMUN_treatchange meanMUN_leftchange meanMUN_treataggch meanMUN_treatagg

save "H:/Stata/Do-files/municipality/maps/mapsmun_map_data", replace
restore
 

** Treated Sorting?

gen spending_national = 39346000000 if year == 1998
replace spending_national = 47888000000 if year == 2002
replace spending_national = 48663000000 if year == 2003
replace spending_national = 49740000000 if year == 2006
replace spending_national = 57003000000 if year == 2010
replace spending_national = 53631000000 if year == 2012
replace spending_national = 58759000000 if year == 2017

bysort munid year: gen spend_mun = spending_national/pop

gen logspend = log(spend_mun)

gen health_national = 2989000000 if year == 1998
replace health_national = 3790000000  if year == 2002
replace health_national = 4135000000 if year == 2003
replace health_national = 4337000000  if year == 2006
replace health_national = 5258000000  if year == 2010
replace health_national = 5359000000 if year == 2012
replace health_national = 4519000000 if year == 2017

bysort munid year: gen spendH_mun = health_national/pop
gen logHspend = log(spendH_mun)


gen education_national = 3713000000 if year == 1998
replace education_national = 4543000000  if year == 2002
replace education_national = 4890000000 if year == 2003
replace education_national = 4537000000  if year == 2006
replace education_national = 3147000000  if year == 2010
replace education_national = 2945000000 if year == 2012
replace education_national = 2821000000 if year == 2017

bysort munid year: gen spendE_mun = education_national/pop
gen logEspend = log(spendE_mun)


gen social_national = 10182000000 if year == 1998
replace social_national = 11606000000  if year == 2002
replace social_national = 12420000000 if year == 2003
replace social_national = 12397000000  if year == 2006
replace social_national = 16578000000  if year == 2010
replace social_national = 16053000000 if year == 2012
replace social_national = 23907000000 if year == 2017

bysort munid year: gen spendS_mun = social_national/pop
gen logSspend = log(spendS_mun)



preserve
gen logpop = log(pop)

replace avhomeprice_adjusted = "" if avhomeprice_adjusted == "NA"
destring avhomeprice_adjusted, replace
drop if avhomeprice_adjusted == . // 29 obs this is the sample for analysis

gen loghome = log(avhomeprice_adjusted)

egen treat_mover_sd = std(treat_movers_pop )
xtreg loghome treat_mover_sd i.year, fe cl(munid)
estimate store s2
regsave treat_mover_sd  using "$temp/regsave_sorting_ncontrols", replace ci
xtreg female_pop treat_mover_sd i.year, fe cl(munid)
estimate store s4
regsave treat_mover_sd  using "$temp/regsave_sorting_ncontrols",  append ci
xtreg logHspend treat_mover_sd i.year, fe cl(munid)
estimate store s7
regsave treat_mover_sd  using "$temp/regsave_sorting_ncontrols",  append ci
xtreg logEspend treat_mover_sd i.year, fe cl(munid)
estimate store s8
regsave treat_mover_sd  using "$temp/regsave_sorting_ncontrols",  append ci
xtreg logSspend treat_mover_sd i.year, fe cl(munid)
estimate store s9
regsave treat_mover_sd  using "$temp/regsave_sorting_ncontrols",  append ci
restore

